package com.qijia.travel.app;

import com.qijia.travel.common.SqlMappedId;
import com.qijia.travel.dao.BaseDAO;
import com.qijia.travel.dao.SingletonDaoFactory;
import com.qijia.travel.dao.TravelDbException;
import com.qijia.travel.domain.PageResult;
import com.qijia.travel.domain.TouristConsumption;
import com.qijia.travel.domain.example.ExampleFactory;
import com.qijia.travel.domain.example.TouristConsumptionExample;
import com.qijia.travel.helper.ObjectHelper;
import com.qijia.travel.vo.TouristGroupVo;

public class ConsumptionManager {
	private BaseDAO baseDao;

	public ConsumptionManager() {
		try {
			baseDao = SingletonDaoFactory.createDAO();
		} catch (TravelDbException e) {
			e.printStackTrace();
		}
	}
	
	public TouristConsumption getConsumptionById(int conId) throws AppErrorException {
		TouristConsumption touristConsumption = new TouristConsumption();
		touristConsumption.setTcId(conId);

		try {
			return (TouristConsumption) baseDao.selectByPrimaryKey(SqlMappedId.CONSUMPTION + "."
					+ SqlMappedId.selectByPrimaryKey, touristConsumption);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取消费细项信息失败!");
		}
	}

	public void createTouristConsumption(TouristConsumption touristConsumption) throws AppErrorException {
		if (touristConsumption == null)
			throw new AppErrorException("请输入新增的消费细项");
		try {
			if (null != touristConsumption.getTcName())
				baseDao.insert(SqlMappedId.CONSUMPTION + "." + SqlMappedId.insert,touristConsumption);
		} catch (TravelDbException e) {
			throw new AppErrorException("注册消费细项失败,请稍候重试!", e);
		}
	}

	public void updateTouristConsumption(TouristConsumption touristConsumption) throws AppErrorException {
		if (touristConsumption == null)
			throw new AppErrorException("请选择要进行修改的消费细项");
		try {
			baseDao.updateByPrimaryKeySelective(SqlMappedId.CONSUMPTION + "."
					+ SqlMappedId.updateByPrimaryKeySelective, touristConsumption);
		} catch (TravelDbException e) {
			e.printStackTrace();
			throw new AppErrorException("更新选中的团队[" + touristConsumption.getTcName() + "]失败,请稍候重试!", e);
		}
	}

	public void deleteTouristConsumption(int tcId) throws AppErrorException {
		if (tcId == 0)
			throw new AppErrorException("请选择要进行删除的团队");
		TouristConsumption touristConsumption = new TouristConsumption();
		try {
			touristConsumption.setTcId(tcId);
			baseDao.deleteByPrimaryKey(SqlMappedId.CONSUMPTION + "." + SqlMappedId.deleteByPrimaryKey, touristConsumption);
		} catch (TravelDbException e) {
			throw new AppErrorException("删除选中的团队[编号为:" + tcId + "]失败,请稍候重试!", e);
		}
	}
	
	public PageResult selectConsumptionListByVo(TouristGroupVo vo) throws AppErrorException{
		TouristConsumptionExample example = ExampleFactory.createConsumptionExample();
		example.createCriteria(vo);
		
		if (vo.getOrderBy() == null || vo.getOrderBy().trim().length() <= 0){
			example.setOrderByClause(" tgd_start_dt desc ");
		} else {
			try {
				example.setOrderByClause(ObjectHelper.getOrderBy(vo
						.getOrderBy(), vo.getACode() == null ? 0 : vo
						.getACode()));
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}

		try {
			PageResult page = PageResult.newInstance();
			page.setSelectDataMappedId(SqlMappedId.CONSUMPTION + "."
					+ SqlMappedId.selectByExample);
			page.setCountMappedId(SqlMappedId.CONSUMPTION + "."
					+ SqlMappedId.countByExample);
			page.setSqlParamObj(example);

			if (vo.getCurPage() == null)
				vo.setCurPage(1);
			else if (vo.getCurPage() != null && vo.getCurPage() <= 1)
				vo.setCurPage(1);

			page.setCurPage(vo.getCurPage());
			page.setPageSize(20);

			return baseDao.selectByPage(page);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取支出信息数据列表失败,请稍候重试!", e);
		} catch (InstantiationException e) {
			throw new AppErrorException("获取支出信息数据列表失败,请稍候重试!", e);
		} catch (IllegalAccessException e) {
			throw new AppErrorException("获取支出信息数据列表失败,请稍候重试!", e);
		}
	}

	public PageResult getConsumptionByToutistId(Integer conId) throws AppErrorException {
		TouristGroupVo vo = new TouristGroupVo();
		vo.setTouristGroupDetailId(conId);
		return selectConsumptionListByVo(vo);
	}
}
